package cn.xie.domain.strategy.service.raffle;

import cn.xie.domain.strategy.model.entity.*;
import cn.xie.domain.strategy.model.vo.RuleFillerCheckVO;
import cn.xie.domain.strategy.repository.IStrategyRepository;
import cn.xie.domain.strategy.service.IRaffleAward;
import cn.xie.domain.strategy.service.armory.IStrategyDispatch;
import cn.xie.domain.strategy.service.rule.factory.DefaultRuleFactory;
import cn.xie.types.enums.ResponseCode;
import cn.xie.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author: xiaoxie
 * create: 2025-08-08 23:47
 * @BelongsProject: bit-market
 * @BelongsPackage: cn.xie.domain.strategy.service.raffle
 * description: 抽奖策略抽象类，定义抽奖的标准流程
 */
@Slf4j
public abstract class AbstractRaffleStrategy implements IRaffleAward {
    protected  IStrategyRepository strategyRepository;

    // 策略调度服务 -> 只负责抽奖处理，通过新增接口的方式，隔离职责，不需要使用方关心或者调用抽奖的初始化
    protected IStrategyDispatch strategyDispatch;


    public AbstractRaffleStrategy(IStrategyRepository strategyRepository,IStrategyDispatch strategyDispatch) {
        this.strategyRepository = strategyRepository;
        this.strategyDispatch = strategyDispatch;
    }

    @Override
    public RaffleAwardEntity raffleAward(RaffleFactorEntity raffleFactorEntity) {
        //校验参数
        String userId = raffleFactorEntity.getUserId();
        Long strategyId = raffleFactorEntity.getStrategyId();
        if(null == userId || null == strategyId){
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }
        // 查询具体的策略
        StrategyEntity strategyEntity = strategyRepository.queryStrategyByStrategyId(strategyId);
        if(null == strategyEntity){
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }
        // 执行抽奖前的策略
        RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> raffleRuleBeforeEntityRuleActionEntity = doStrategyRuleFiller(RuleFillerEntity.builder()
                .userId(userId).strategyId(strategyId).build(), strategyEntity.getRuleModels());
        // 查看是否被规则引擎接管
        if(RuleFillerCheckVO.TAKE_OVER.getCode().equals(raffleRuleBeforeEntityRuleActionEntity.getCode())) {
            // 先执行黑名单 -黑名单不需要抽奖动作 直接返回奖品id
            if(DefaultRuleFactory.RuleModel.RULE_BLACK_LIST.getCode().equals(raffleRuleBeforeEntityRuleActionEntity.getRuleModel())) {
                return RaffleAwardEntity.builder()
                        .awardId(raffleRuleBeforeEntityRuleActionEntity.getData().getAwardId())
                        .build();
                // 再执行权重规则的抽奖
            }else if(DefaultRuleFactory.RuleModel.RULE_WEIGHT.getCode().equals(raffleRuleBeforeEntityRuleActionEntity.getRuleModel())){
                RuleActionEntity.RaffleRuleBeforeEntity data = raffleRuleBeforeEntityRuleActionEntity.getData();
                Integer awardId = strategyDispatch.getRandomAwardId(strategyId, data.getRuleWeightValue());
                return RaffleAwardEntity.builder()
                        .awardId(awardId)
                        .build();
            }

        }
        // 没有执行规则引擎的抽奖
        Integer awardId = strategyDispatch.getRandomAwardId(strategyId);
        return RaffleAwardEntity.builder()
                .awardId(awardId)
                .build();

    }

    protected abstract RuleActionEntity<RuleActionEntity.RaffleRuleBeforeEntity> doStrategyRuleFiller(RuleFillerEntity build, String... ruleModel);

}
